package com.example.manager.ui;


import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;


import com.example.manager.Adapter.SlidePagerAdapter;
import com.example.manager.R;
import com.example.manager.javaBean.GsonBean;
import com.example.manager.retrofit.ApiService;
import com.example.manager.Utils.RetrofitManager;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private Button music;


    private SlidePagerAdapter slidePagerAdapter;
    private List<String> advPic;
    private static final int AUTO_SCROLL_DELAY = 3000; // 设置自动轮播的间隔时间，单位毫秒
    private Handler autoScrollHandler = new Handler();
    private Runnable autoScrollRunnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        viewPager = findViewById(R.id.viewPager);

        music = findViewById(R.id.music);
        music.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), musicActivity.class));
            }
        });
        initData();
        setupAutoScroll();

    }


    private void initData() {
        get_banner();
    }

    private void get_banner() {
        ApiService apiService = RetrofitManager.getApiService();
        apiService.getBanner("2").enqueue(new Callback<GsonBean>() {
            @Override
            public void onResponse(Call<GsonBean> call, Response<GsonBean> response) {
                List<String> imageList = new ArrayList<>();
                for (int i = 0; i < response.body().rows.size(); i++) {
                    String adv = RetrofitManager.BASE_URL + response.body().rows.get(i).advImg;
                    imageList.add(adv);
                }
                slidePagerAdapter = new SlidePagerAdapter(HomeActivity.this, imageList);
                viewPager.setAdapter(slidePagerAdapter);
            }

            @Override
            public void onFailure(Call<GsonBean> call, Throwable throwable) {

            }

            private void setBanner() {

            }


        });

    }

    private void setupAutoScroll() {
        autoScrollRunnable = new Runnable() {
            @Override
            public void run() {
                int currentItem = viewPager.getCurrentItem();
                int nextItem = (currentItem + 1) % slidePagerAdapter.getCount();
                viewPager.setCurrentItem(nextItem, true);
                autoScrollHandler.postDelayed(this, AUTO_SCROLL_DELAY);
            }
        };

        // 第一次启动自动轮播
        autoScrollHandler.postDelayed(autoScrollRunnable, AUTO_SCROLL_DELAY);
    }

    // 在 onDestroy 方法中停止自动轮播
    @Override
    protected void onDestroy() {
        super.onDestroy();
        autoScrollHandler.removeCallbacks(autoScrollRunnable);
    }
}